package ecBase;
import java.util.*;

/*
 * Population.java
 * Created: 2005-11-30 10:20
 */
/**
* A population is a collection of Individuals in the EC population.
  *
 * @author T.S.Yo
 * @last updated 2005/11/30
**/

public class Population{
    private int popSize = 100;
    public Individual[] population;
    
// Constructor    
    /** Random Constructor  with default size**/
    public Population(){
        population = new Individual[popSize];
        randInitialization();
    }

    /** Random Constructor with given size**/
    public Population(final int size){
        popSize = size;
        population = new Individual[popSize];
        randInitialization();
    }

    /** Copy Constructor **/
    public Population(final Population pop){
        popSize = pop.getSize();
        population = new Individual[popSize];
        for (int i = 0; i < popSize; i++){
            population[i] = pop.getSpecifiedInd(i);
        }
    }
//
// Methods to access private members    
    /** Returning the popSize**/
    public int getSize(){
        return popSize;
    }
    /** The popSize cannot be changed after initialization **/
    public void setSize(final int size){
        System.out.println("The popSize cannot be changed after initialization");
    }

    /** Randomly initializing the population **/
    public void randInitialization(){
        for (int i = 0; i < popSize; i++){
            population[i] = new Individual();
        }
    }
    
    /** Returning a specific individual in the population **/
    public Individual getSpecifiedInd(final int index){
        return population[index];
    }
    /** Modifying a specific individual in the population **/
    public void setSpecifiedInd(final int index, final Individual ind){
        population[index].setGenes(ind);
    }

    /** Reordering the population by Quick-sort algorithm **/
    public void sortPopulation(){
        Arrays.sort(population, new Individual());
    }
//
// Information about the population
    /** Show the population **/
    public void showPopulation(){
        System.out.println("Population: ["+this+"]    Size: "+popSize+"    Fitness: "+fitness());
        System.out.println("----------------------------------------");
        System.out.println("ID        Indivudual                Fitness");
        System.out.println("----------------------------------------");
        for (int i = 0; i < popSize; i++){
            System.out.println(i+"    "+population[i]+"    "+population[i].fitness());
        }
        System.out.println("----------------------------------------");
        System.out.println("");
    }

    /** Calculate averaged fitness of the population **/
    public double fitness(){
        double popFitness = 0.0;
        for (int i = 0; i < popSize; i++){
            popFitness += population[i].fitness();
        }
        popFitness /= (double)popSize;
        return popFitness;
    }
}